package com.example.com.skills_utf8;

import inject.ViewInject;

import java.lang.reflect.Field;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.utils.Utils1;



// http://blog.csdn.net/zhangweiwtmdbf/article/details/30246149	
public class _38Activity extends Activity{


	_38Activity activity = this;
	
	
	@ViewInject(R.id.tv_title)
	TextView tv_title;	
	
	@ViewInject(R.id.btn_result)
	Button btn_result;
	
	@ViewInject(R.id.et_text)
	EditText et_text;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_38);
		
		autoInjectAllField();

		tv_title.setText("标题");
		btn_result.setText("结果");
		et_text.setText("..........");
		
		btn_result.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Utils1.showToastShort(activity, "点击~");
			}
		});
		
		
	}
	
	
	
	
	/**
	 * 解析注解
	 */
	public void autoInjectAllField() {
		try {
			Class<?> clazz = this.getClass();
			Field[] fields = clazz.getDeclaredFields();//获得Activity中声明的字段
			for (Field field : fields) {
				// 查看这个字段是否有我们自定义的注解类标志的
				if (field.isAnnotationPresent(ViewInject.class)) {
					ViewInject inject = field.getAnnotation(ViewInject.class);
					int id = inject.value();
					if (id > 0) {
						field.setAccessible(true);
						field.set(this, this.findViewById(id));//给我们要找的字段设置值
					}
				}
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
	}
	
	
}
